package com.utils.script.shell;

import org.apache.commons.lang.ArrayUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

/**
 * java执行shell脚本
 * @author XR-8
 *
 */
public class ExecuteShell {
	
	public static void main(String[] args) {
        execShell(true, "{\"date\":\"2017-11-29 14:40:54\",\"severity\":\"MAJOR\",\"mc_object\":\"CELL:10.193.22.239:182\",\"msg\":\"Cell on 10.193.22.239:1828 is disconnected from Integration Server 10.193.16.183:12125.\",\"mc_appname\":\"\",\"mc_tool_class\":\"NGMS_BPPM\",\"mc_host_address\":\"10.193.16.183\",\"event_handle\":\"1118702\",\"AREA\":\"田林中心\",\"mc_parameter_value\":\"85.06\",\"mc_parameter\":\"CPUprcrProcessorTimePercent\",\"mc_object_class\":\"PROACTIVENET_COMPONENT\",\"SOURCE_IP\":\"10.193.16.176\",\"OCTOPUS\":\"34\",\"updatetime\":\"2017-11-29 16:29:55\",\"status\":\"BLACKOUT\"} ");

	}
	
	/**
     * 执行shell
     * @param execCmd 使用命令 或 脚本标志位
     * @param para 传入参数
     */
    private static void execShell(boolean execCmd, String... para) {
        StringBuffer paras = new StringBuffer();
        Arrays.stream(para).forEach(x -> paras.append(x).append(" "));
        try {
            String cmd = "", shpath = "";
            if (execCmd) {
                // 命令模式
                shpath = "echo";
            } else {
            //脚本路径
                shpath = "/Users/yangyibo/Desktop/callShell.sh";

            }
            cmd = shpath + " " + paras.toString();
            Process ps = Runtime.getRuntime().exec(cmd);
            ps.waitFor();

            BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line).append("\n");
            }
            String result = sb.toString();
            System.out.println(result);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 解决了 参数中包含 空格和脚本没有执行权限的问题
     * @param scriptPath 脚本路径
     * @param para 参数数组
     */
    private String execShell(String scriptPath, String ... para) {
        try {
            String[] cmd = new String[]{scriptPath};
            //为了解决参数中包含空格
//            cmd= ArrayUtils.addAll(cmd,para);

            //解决脚本没有执行权限
            ProcessBuilder builder = new ProcessBuilder("/bin/chmod", "755",scriptPath);
            Process process = builder.start();
            process.waitFor();

            Process ps = Runtime.getRuntime().exec(cmd);
            ps.waitFor();
            //1.我能问一个问题吗java进行一直等待shell返回 如果shell执行时间过长怎么办啊？一直等待保持链接？
            //ps.waitFor(100, TimeUnit.SECONDS);
            
            //2.注意：不要在后面添加waitFor()。即：java程序不等待run.sh的执行结果。

            BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line).append("\n");
            }
            //执行结果
            String result = sb.toString();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return "执行失败："+e.getMessage();
        }
    }

}
